import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:common_utils/common_utils.dart';
import 'package:drm/util/utils.dart';

class SearchDate {
  String ksrq;
  String jsrq;

  SearchDate({this.ksrq, this.jsrq});
}

class DateTimeDialog {
  static Picker iniPicker(DateTime inivalue,
      {bool hideHeader = false,
      int type = PickerDateTimeType.kYMD,
      Function onSuccess(DateTime dateTime)}) {
    return Picker(
        hideHeader: hideHeader,
        adapter: DateTimePickerAdapter(
            type: type,
            isNumberMonth: true,
            value: inivalue,
            yearSuffix: '年',
            monthSuffix: '月',
            daySuffix: '日'),
        onConfirm: (Picker picker, List value) {
          inivalue = (picker.adapter as DateTimePickerAdapter).value;
//          onSuccess(iniDateTime);
        });
  }

  // 开始日期，结束日期；
  static Future<SearchDate> showPickerDateRange(
      BuildContext context, String ksrq, String jsrq) {
    DateTime ksDateTime;
    DateTime jsDateTime;
    if (ksrq.isNotEmpty) {
      ksDateTime = DateUtil.getDateTime(ksrq);
    } else {
      ksDateTime = DateTime.now();
    }
    if (jsrq.isNotEmpty) {
      jsDateTime = DateUtil.getDateTime(jsrq);
    } else {
      jsDateTime = DateTime.now();
    }

    Picker ps = Picker(
        hideHeader: true,
        adapter: DateTimePickerAdapter(
            type: PickerDateTimeType.kYMD,
            isNumberMonth: true,
            value: ksDateTime,
            yearSuffix: '年',
            monthSuffix: '月',
            daySuffix: '日'),
        onConfirm: (Picker picker, List value) {
          ksDateTime = (picker.adapter as DateTimePickerAdapter).value;
        });

    Picker pe = Picker(
        hideHeader: true,
        adapter: DateTimePickerAdapter(
            type: PickerDateTimeType.kYMD,
            isNumberMonth: true,
            value: jsDateTime,
            yearSuffix: '年',
            monthSuffix: '月',
            daySuffix: '日'),
        onConfirm: (Picker picker, List value) {
          jsDateTime = (picker.adapter as DateTimePickerAdapter).value;
        });

    List<Widget> actions = [
      FlatButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: new Text('取消')),
      FlatButton(
          onPressed: () {
            ps.onConfirm(ps, ps.selecteds);
            pe.onConfirm(pe, pe.selecteds);

            if (ksDateTime.compareTo(jsDateTime) > 0) {
              MyUtils.showToast('开始日期不能大于结束日期');
            } else {
              Navigator.of(context).pop(SearchDate(
                  ksrq: DateUtil.getDateStrByDateTime(ksDateTime,
                      format: DateFormat.YEAR_MONTH_DAY),
                  jsrq: DateUtil.getDateStrByDateTime(jsDateTime,
                      format: DateFormat.YEAR_MONTH_DAY)));
            }
          },
          child: new Text('确定'))
    ];

    return showDialog<SearchDate>(
        context: context,
        builder: (BuildContext context) {
          return new AlertDialog(
            title: Text(""),
            actions: actions,
            content: Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Text("开始日期:"),
                  ps.makePicker(),
                  Divider(),
                  Text("结束日期:"),
                  pe.makePicker()
                ],
              ),
            ),
          );
        });
  }

  //

  static showPickerYearMonthDate(BuildContext context, String year,
      String month, onSuccess(String select)) {
    Picker(
        hideHeader: true,
        adapter: DateTimePickerAdapter(
          type: PickerDateTimeType.kYM,
          isNumberMonth: true,
          value: DateTime(year.isEmpty ? DateTime.now().year : int.parse(year),
              month.isEmpty ? DateTime.now().month : int.parse(month)),
          yearSuffix: '年',
          monthSuffix: '月',
        ),
        onConfirm: (Picker picker, List value) {
          DateTime select = (picker.adapter as DateTimePickerAdapter).value;
          String month =
              select.month < 10 ? '0${select.month}' : '${select.month}';
          onSuccess('${select.year}-$month');
        }).showDialog(context);
  }

  static showYearPicker(
      BuildContext context, String year, onSuccess(String select),
      {int beginYear = -1, int endYear = -1}) {
    DateTime now = DateTime.now();
    if (beginYear == -1) {
      beginYear = now.year - 50;
    }
    if (endYear == -1) {
      endYear = now.year + 50;
    }
    List<int> years = List();
    int iniYear = beginYear;
    try {
      if (year.isNotEmpty) {
        iniYear = int.parse(year);
        if (iniYear < beginYear) {
          iniYear = beginYear;
        }
      }
    } catch (e) {}

    for (int i = beginYear; i <= endYear; i++) {
      years.add(i);
    }

    NumberPickerColumn item = NumberPickerColumn(
        begin: beginYear, end: endYear, items: years, initValue: iniYear);

    Picker(
        hideHeader: true,
        adapter: NumberPickerAdapter(
          data: [item],
        ),
        onConfirm: (picker, List<int> items) {
          List<int> selectValue = picker.getSelectedValues();
          if (selectValue != null && selectValue.length > 0) {
            onSuccess('${selectValue[0]}');
          } else {
            onSuccess('');
          }
        }).showDialog(context);
  }



  static showPickerFullDate(BuildContext context,String dateString, onSuccess(String select)) {
    DateTime time = DateTime.now();
    if (dateString.isNotEmpty) {
      try {
        time = DateTime.parse(dateString);
      }catch(e) {
        print(e.toString());
      }
    }

    Picker(
        hideHeader: true,
        adapter: DateTimePickerAdapter(
          type: PickerDateTimeType.kYMDHM,
          isNumberMonth: true,
          value: time,

        ),
        onConfirm: (Picker picker, List value) {
          DateTime select = (picker.adapter as DateTimePickerAdapter).value;

          onSuccess('${select.year}-${MyUtils.addNing(select.month)}-${MyUtils.addNing(select.day)} ${MyUtils.addNing(select.hour)}:${MyUtils.addNing(select.minute)}');
        }).showDialog(context);
  }
}
